<template>
  <el-container class="container" v-cloak v-if="loginSign">
    <el-header class="header">
      <div class="header-left">
        <img class="ico" src="favicon.ico">
        <span class="title">Management System </span>
      </div>
      <div class="header-right">
        <span class="hello">Hello, {{ username }}</span>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu :default-active="activeIndex" class="menu" router background-color="#545c64" text-color="#fff"
          active-text-color="#ffd04b">
          <el-menu-item index="/Map">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>Map</span>
            </template>
          </el-menu-item>
          <el-menu-item index="/User">
            <i class="el-icon-menu"></i>
            <span slot="title">User</span>
          </el-menu-item>
          <el-menu-item index="/Point">
            <i class="el-icon-document"></i>
            <span slot="title">Point</span>
          </el-menu-item>
          <el-submenu index="">
            <template slot="title">
              <i class="el-icon-notebook-1"></i>
              <span>Apply for</span>
            </template>
            <el-menu-item index="/Apply_admin">
              <i class="el-icon-document-checked"></i>
              <span slot="title">Admin</span>
            </el-menu-item>
            <el-menu-item index="/Apply_point">
              <i class="el-icon-document-checked"></i>
              <span slot="title">Point</span>
            </el-menu-item>
          </el-submenu>

          <el-menu-item index="" class="logout" @click="logout()">
            <i class="el-icon-position"></i>
            <span slot="title">Logout</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { IsLoginAPI, LogoutAPI } from '@/api'
export default {
  name: 'HomeView',
  data() {
    return {
      loginSign: false,
      activeIndex: this.$route.path
    }
  },
  computed: {
    username: function () {
      return window.localStorage.getItem('username')
    }
  },
  methods: {
    // Check if you are logged in
    async isLogin() {
      IsLoginAPI().then((res) => {
        if (res.data.code != 0) {
          return this.toLogin();
        }
        else if (res.data.role != 3) {
          this.$notify.warning({ title: "Access denied, insufficient user permissions" });
          return this.toLogin();
        }
        this.loginSign = true
      }).catch((e) => { this.toLogin(); });
    },
    // safe logout
    async logout() {
      let { data } = await LogoutAPI();
      this.$notify.success({ title: data.msg });
      this.toLogin();
    },
    // Jump to login page
    toLogin() {
      this.$router.push('login');
    }
  },
  async mounted() {
    await this.isLogin();
  }
}
</script>

<style scoped>
.header {
  background-color: #3b4247;
  display: flex;
  justify-content: space-between;
  color: #fff;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
}

.ico {
  width: 40px;
  height: 40px;
  margin: 10px;
  border-radius: 40px;
}

.title {
  font-size: 24px;
}

.hello {
  font-size: 20px;
}

.menu {
  height: calc(100vh - 60px);
}

.main {
  height: calc(100vh - 60px);
}

.logout {
  position: absolute;
  bottom: 10px;
  left: 0;
  right: 0;
}
</style>